package com.xiaogao.serviceCms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaogao.commonUtils.Result;
import com.xiaogao.serviceCms.entity.CrmBanner;
import com.xiaogao.serviceCms.service.CrmBannerService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 后台控制器
 * </p>
 *
 * @author 小高
 * @since 2021-06-24
 */
@RestController
@RequestMapping("/serviceCms/bannerAdmin")
public class BannerAdminController {

    @Autowired
    private CrmBannerService bannerService;

    @ApiOperation(value = "获取banner分页列表")
    @GetMapping("/index/{page}/{limit}")
    public Result index(
            @ApiParam(name = "page",value = "当前页",required = true)
            @PathVariable("page")Long page,

            @ApiParam(name = "limit",value = "每页显示数",required = true)
            @PathVariable("limit")Long limit){

        Page<CrmBanner> pageParam = new Page<>(page,limit);
        bannerService.page(pageParam,null);
        System.out.println(pageParam.getTotal());
        return Result.ok().data("items",pageParam.getRecords()).data("total",pageParam.getTotal());
    }

    @ApiOperation(value = "获取Banner")
    @GetMapping("/getBanner/{id}")
    public Result getBanner(@PathVariable("id")String id){
        CrmBanner banner = bannerService.getById(id);
        return Result.ok().data("item",banner);
    }

    @ApiOperation(value = "新增Banner")
    @PostMapping("/addBanner")
    public Result addBanner(@RequestBody CrmBanner crmBanner){
        bannerService.save(crmBanner);
        return Result.ok();
    }

    @ApiOperation(value = "修改Banner")
    @PostMapping("/updateBanner")
    public Result updateBanner(@RequestBody CrmBanner crmBanner){
        bannerService.updateById(crmBanner);
        return Result.ok();
    }

    @ApiOperation(value = "删除Banner")
    @DeleteMapping("/removeBanner/{id}")
    public Result removeBanner(@PathVariable("id")String id){
        bannerService.removeById(id);
        return Result.ok();
    }

}

